class Spoj1AdminController < ApplicationController
  layout 'standart'
  include Authorizer
  include AdminModule
  before_filter :login_required 
  def index
  end

  def add_admin
    @users = User.find(:all,:conditions => not_admin_sql)
  end

  def administrate
    if params[:user].nil?
      flash[:error] = "Could not select user! Please try again!"
      redirect_to :action => :add_admin
    end
    @user = User.find_by_user_id(params[:user])
    make_admin @user
    if @user.save
      flash[:notice] = "User successfully made administrator!"
      redirect_to :action => :index
    else
      flash[:error] = "Could not make the user admin! Please try again!"
      redirect_to :action => :add_admin
    end
  end
  
  def select_problem_to_edit
    @problems = Problem.find(:all)
    @problem =params[:problem_to_edit]? Problem.find_by_problem_id(params[:problem_to_edit]):nil 
    @contest = @problem.contest if @problem
  end
  
  def select_contest_to_edit
    @contests = Contest.find(:all)
    @options = @contests.map{|t| [t.name,t.contest_id]}
  end

  def do_edit_problem
    p = Problem.find_by_problem_id(params[:id])
    if p
      redirect_to edit_problem_path(p)
    else
      flash[:error] = "No problem selected"
      render :action => :select_problem_to_edit 
    end   
  end
  
  def do_edit_contest
    c = Contest.find_by_contest_id(params[:id])
    if c
      redirect_to edit_contest_path(c)
    else
      flash[:error] = "No contest selected"
      render :action => :select_contest_to_edit
    end   
  end


  def rejudge_all_runs
    r = Run.find(:all)
    flag = true
    r.each do |run|
      # Judge програмата периодично обработва всички решения с такъв статут
      run.status = "waiting..."
      flag = false unless run.save
    end
    if flag
      flash[:notice] = "Rejudge all runs successfully initiated"
    else
      flash[:error] = "Could not rejudge all runs"
    end
    redirect_to :controller => :spoj1, :action => :status 
  end

  def rejudge_run
    r = Run.find_by_run_id(params[:id])
    r.status = "waiting..."
    unless r.save
      flash[:error] = "Could not rejudge run"
      render :back
    else
      flash[:notice] = "Rejudge run sucessfully initiated"
      redirect_to :back
    end
    
  end
  def show_problem
      @problem = Problem.find_by_problem_id(params[:problem])
  end
  
  
  def show_contest
    @contest = params[:contest]
    @problem = params[:problem]
  end

  def view_board
    config = load_config
    unless params[:contest] 
      flash[:error] = "No contest selected!"
      redirect_to :root
    else
      @contest = Contest.find_by_contest_id(params[:contest])
      unless @contest
        flash[:error] = "The selected contest does not exist"
        redirect_to :action => :index
      end
    end
    @problems = Problem.find(:all,:conditions => {:contest_id => @contest})
    @users = User.find(:all,:conditions => {:user_id => 
          Run.find(:all,:select => "DISTINCT user_id",:conditions => {:problem_id => @problems}).map{|u| u.user_id}})
    @results = {}
    @users.each do |u|
      @results[u.name] = {:all => 0,:attempts => 0,:time => 0}
      @problems.each do |p|
        o = Run.find(:first,:order => :submit_time,:conditions =>["user_id = ? AND problem_id = ? AND status = ? AND
            submit_time >= ?",u,p,config['status']['ok'],@contest.start_time])
        if o
          success_time = ((o.submit_time - @contest.start_time)/60).to_i
          unsuccessfull = Run.find(:all,:conditions =>["user_id = ? AND problem_id = ? 
          AND status IN(?) AND submit_time <= ? AND submit_time >= ? ",u,p,wrong_status,o.submit_time,@contest.start_time])
          @results[u.name][p.problem_id] = ok_status_string(unsuccessfull.size,success_time)
          @results[u.name][:all] += 1
          @results[u.name][:attempts] += unsuccessfull.size+1
          @results[u.name][:time] += success_time + unsuccessfull.size*config['wrong_submit_penalty']
        else
          unsuccessfull = Run.find(:all,:order => "submit_time DESC",:conditions => {:user_id => u,:problem_id => p,:status =>
                wrong_status})
          wa_time = 0
          if unsuccessfull.size > 0
            wa_time =  ((unsuccessfull[0].submit_time - @contest.start_time)/60).to_i
          end          
          @results[u.name][p.problem_id] = wrong_status_string(unsuccessfull.size,wa_time)
          @results[u.name][:attempts] += unsuccessfull.size
        end
      end
    end
    @results = @results.sort_by{|k,v| [-v[:all],v[:time]]}
  end


end
